package com.mayuhao.coursemanage.util;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author 马宇豪
 * @description 请修改方法描述
 * @methodName
 * @params
 * @paramType
 * @return
 * @date 2022/10/25 0025 下午 9:02
 * @email 1757442521@qq.com
 **/

public class CodeGenerator {
    /**
     * @return void
     * @description 请修改方法描述
     * @methodName codeGenerator
     * @params []
     * @paramType []
     * @author 马宇豪
     * @date 2022/10/25 0025 下午 9:02
     * @email 1757442521@qq.com
     **/
    public static void codeGenerator() {
        String url = "jdbc:mysql://localhost:3306/course_selection_system?serverTimezone=UTC&useUnicode=ture&characterEncoding=utf-8";
        String pkgPath = System.getProperty("user.dir") + "/course-manage/src/main/java";
        String pkgXml = System.getProperty("user.dir") + "/course-manage/src/main/resources/mapper";
        FastAutoGenerator.create(url, "root", "20010810")
                // 全局配置
                .globalConfig((scanner, builder) -> builder.outputDir(pkgPath).author(scanner.apply("请输入作者名称？")).fileOverride().disableOpenDir())
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？"))
                        .pathInfo(Collections.singletonMap(OutputFile.xml, pkgXml)))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder().enableLombok()
                        .addTableFills(new Column("create_time", FieldFill.INSERT)
                        ).build())
                .execute();
    }

    // 处理 all 情况

    /**
     * @param tables
     * @return
     */
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {
        codeGenerator();
    }
}

